﻿#include "GUILoadPointForceDialog.h"
#include "ui_GUILoadPointForceDialog.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKLoadExcitationForceMoments.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"

namespace GUI {
    GUILoadPointForceDialog::GUILoadPointForceDialog(Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUILoadBaseDialog(nullptr, oper, parent),
        _ui(new Ui::GUILoadPointForceDialog)
    {
        _ui->setupUi(this);
        //创建新载荷数据
        this->initWidgetImage();
        this->createNewLoadPointForceObjData();
        this->init();
        //设置窗口自适应隐藏组件
        this->layout()->setSizeConstraint(QLayout::SetMinimumSize);
    }

    GUILoadPointForceDialog::GUILoadPointForceDialog(Acoustics::FITKAcousticsAbstractLoad* obj, Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUILoadBaseDialog(obj, oper, parent),
        _ui(new Ui::GUILoadPointForceDialog)
    {
        _ui->setupUi(this);
        //初始化 
        this->initWidgetImage();
        this->init();
        //设置窗口自适应隐藏组件
        this->layout()->setSizeConstraint(QLayout::SetMinAndMaxSize);
    }

    GUILoadPointForceDialog::~GUILoadPointForceDialog()
    {
        //释放数据
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUILoadPointForceDialog::init()
    {
        if (!_obj) return;
        //初始化数据
        QString name = _obj->getDataObjectName();
        if (name.isEmpty())
        {
            name = "ForceMoment-1";
            name = this->ckeckNameFromMgr(name);
        }
        QString connectionName = _obj->getConnectionObjName();
        bool isEnabledState = _obj->getEnabledState();
        _ui->lineEdit_Name->setText(name);
        _ui->lineEdit_Connection->setText(connectionName);
        _ui->checkBox_EnabledState->setCheckState((isEnabledState ? Qt::CheckState::Checked : Qt::CheckState::Unchecked));
        //获取类型
        bool isSeaModel = false;
        int seaType = 0;
        if (!this->getPickDiscDataType(_obj->getConnectionObjID(), isSeaModel, seaType)) return;
        //初始化数据
        if (isSeaModel)
            this->initSEALoadPointForceData();
        else
            this->initFELoadPointForceData();
    }

    void GUILoadPointForceDialog::on_pushButton_Cancel_clicked()
    {
        //点击取消按键动作
        this->reject();
    }

    void GUILoadPointForceDialog::on_pushButton_OK_clicked()
    {
        if (!_oper)return;
        //获取操作器动作类型
        QString operType;
        _oper->argValue<QString>("OperModelType", operType);
        if (operType.isEmpty())
        {
            delete _obj;
            this->reject();
            return;
        }
        //创建动作
        if (operType == "Create")
        {
            if (!this->createObjData())
                return;
        }
        //修改动作
        else if (operType == "Edit")
        {
            if (!this->editObjData())
                return;
        }
        this->accept();
    }

    void GUILoadPointForceDialog::on_pushButton_F_Preset_X_clicked()
    {
        //Force Preset X button clicked slot
        _ui->lineEdit_F_Ori_X->setText("1");
        _ui->lineEdit_F_Ori_Y->setText("0");
        _ui->lineEdit_F_Ori_Z->setText("0");
    }

    void GUILoadPointForceDialog::on_pushButton_F_Preset_Y_clicked()
    {
        //Force Preset Y button clicked slot
        _ui->lineEdit_F_Ori_X->setText("0");
        _ui->lineEdit_F_Ori_Y->setText("1");
        _ui->lineEdit_F_Ori_Z->setText("0");
    }

    void GUILoadPointForceDialog::on_pushButton_F_Preset_Z_clicked()
    {
        //Force Preset Z button clicked slot
        _ui->lineEdit_F_Ori_X->setText("0");
        _ui->lineEdit_F_Ori_Y->setText("0");
        _ui->lineEdit_F_Ori_Z->setText("1");
    }

    void GUILoadPointForceDialog::on_pushButton_M_Preset_X_clicked()
    {
        //Moment Preset X button clicked slot
        _ui->lineEdit_M_Ori_X->setText("1");
        _ui->lineEdit_M_Ori_Y->setText("0");
        _ui->lineEdit_M_Ori_Z->setText("0");
    }

    void GUILoadPointForceDialog::on_pushButton_M_Preset_Y_clicked()
    {
        //Moment Preset Y button clicked slot
        _ui->lineEdit_M_Ori_X->setText("0");
        _ui->lineEdit_M_Ori_Y->setText("1");
        _ui->lineEdit_M_Ori_Z->setText("0");
    }

    void GUILoadPointForceDialog::on_pushButton_M_Preset_Z_clicked()
    {
        //Moment Preset Z button clicked slot
        _ui->lineEdit_M_Ori_X->setText("0");
        _ui->lineEdit_M_Ori_Y->setText("0");
        _ui->lineEdit_M_Ori_Z->setText("1");
    }

    void GUILoadPointForceDialog::initSEALoadPointForceData()
    {
        _ui->tabWidget_2->setVisible(false);
        //初始化力与力矩
        this->setComboBoxSpectra(QList<QComboBox*>() << _ui->comboBox_ForceSpectra, QList<int>() << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Force);
        this->setComboBoxSpectra(QList<QComboBox*>() << _ui->comboBox_MomentSpectra, QList<int>() << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Moment);
        Acoustics::FITKLoadExcitationForceMoments* forceMoment = dynamic_cast<Acoustics::FITKLoadExcitationForceMoments*>(_obj);
        if (!forceMoment) return;
        //初始化频谱
        int forceSpectraID = forceMoment->getForceSpectraID();
        int index = _ui->comboBox_ForceSpectra->findData(forceSpectraID);
        _ui->comboBox_ForceSpectra->setCurrentIndex((index < 0 ? 0 : index));
        int momentSpectraID = forceMoment->getMomentSpectraID();
        index = _ui->comboBox_MomentSpectra->findData(momentSpectraID);
        _ui->comboBox_MomentSpectra->setCurrentIndex((index < 0 ? 0 : index));
        //初始化位置
        double fOriVec[3]{ 0,0,0 }, mOriVec[3]{ 0,0,0 }, connAxes[3]{ 0,0,0 };
        forceMoment->getForceOrientationVec(fOriVec);
        forceMoment->getMomentOrientationVec(mOriVec);
        forceMoment->getOffsetValue(connAxes);
        _ui->lineEdit_F_Ori_X->setText(QString::number(fOriVec[0]));
        _ui->lineEdit_F_Ori_Y->setText(QString::number(fOriVec[1]));
        _ui->lineEdit_F_Ori_Z->setText(QString::number(fOriVec[2]));
        _ui->lineEdit_M_Ori_X->setText(QString::number(mOriVec[0]));
        _ui->lineEdit_M_Ori_Y->setText(QString::number(mOriVec[1]));
        _ui->lineEdit_M_Ori_Z->setText(QString::number(mOriVec[2]));
        _ui->lineEdit_dx->setText(QString::number(connAxes[0]));
        _ui->lineEdit_dy->setText(QString::number(connAxes[1]));
        _ui->lineEdit_dz->setText(QString::number(connAxes[2]));
        //设置间接辐射状态
        bool indirectRadiation = forceMoment->getIndirectRadiation();
        _ui->checkBox_IndirectRadiation->setCheckState((indirectRadiation ? Qt::CheckState::Checked : Qt::CheckState::Unchecked));
    }

    void GUILoadPointForceDialog::initFELoadPointForceData()
    {
        _ui->tabWidget->setVisible(false);
        //初始化力与力矩
        this->setComboBoxSpectra(QList<QComboBox*>() << _ui->comboBox_FEForeSpectrum, QList<int>() << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Force);
        this->setComboBoxSpectra(QList<QComboBox*>() << _ui->comboBox_FEMomentSpectrum, QList<int>() << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Moment);
        Acoustics::FITKLoadExcitationForceMoments* forceMoment = dynamic_cast<Acoustics::FITKLoadExcitationForceMoments*>(_obj);
        if (!forceMoment) return;
        //初始化频谱
        int forceSpectraID = forceMoment->getForceSpectraID();
        int index = _ui->comboBox_FEForeSpectrum->findData(forceSpectraID);
        _ui->comboBox_FEForeSpectrum->setCurrentIndex((index < 0 ? 0 : index));
        int momentSpectraID = forceMoment->getMomentSpectraID();
        index = _ui->comboBox_FEMomentSpectrum->findData(momentSpectraID);
        _ui->comboBox_FEMomentSpectrum->setCurrentIndex((index < 0 ? 0 : index));
        //初始化位置
        double fOriVec[3]{ 0,0,0 }, mOriVec[3]{ 0,0,0 };
        forceMoment->getForceOrientationVec(fOriVec);
        forceMoment->getMomentOrientationVec(mOriVec);
        _ui->lineEdit_LxForce->setText(QString::number(fOriVec[0]));
        _ui->lineEdit_LyForce->setText(QString::number(fOriVec[1]));
        _ui->lineEdit_LzForce->setText(QString::number(fOriVec[2]));
        _ui->lineEdit_LxMoment->setText(QString::number(mOriVec[0]));
        _ui->lineEdit_LyMoment->setText(QString::number(mOriVec[1]));
        _ui->lineEdit_LzMoment->setText(QString::number(mOriVec[2]));
        //初始化节点
        int nodeID = forceMoment->getFENodeID();
        _ui->lineEdit_NodeID->setText(QString::number(nodeID));
    }

    void GUILoadPointForceDialog::initWidgetImage()
    {
        //初始化图像
        _ui->label->setPixmap(QPixmap(":/ToolBar/toolBar/Load_Force_and_Moment.png"));
        _ui->label_F_Image_X->setPixmap(QPixmap(":/ToolBar/toolBar/Load_Force_and_Moment.png"));
        _ui->label_F_Image_Y->setPixmap(QPixmap(":/ToolBar/toolBar/Load_Force_and_Moment.png"));
        _ui->label_F_Image_Z->setPixmap(QPixmap(":/ToolBar/toolBar/Load_Force_and_Moment.png"));
        _ui->label_M_Image_X->setPixmap(QPixmap(":/ToolBar/toolBar/Load_Force_and_Moment.png"));
        _ui->label_M_Image_Y->setPixmap(QPixmap(":/ToolBar/toolBar/Load_Force_and_Moment.png"));
        _ui->label_M_Image_Z->setPixmap(QPixmap(":/ToolBar/toolBar/Load_Force_and_Moment.png"));
        _ui->label_Connect_Image->setPixmap(QPixmap(":/ToolBar/toolBar/Load_Force_and_Moment.png"));
    }

    void GUILoadPointForceDialog::createNewLoadPointForceObjData()
    {
        //获取拾取数据
        if (!_oper) return;
        int pickObjID = 0;
        if (!_oper->argValue<int>("PickObjID", pickObjID)) return;
        double connectionLocation[3]{ 0.0,0.0,0.0 };
        if (!_oper->argValue<double>("connectPointX", connectionLocation[0])) return;
        if (!_oper->argValue<double>("connectPointY", connectionLocation[1])) return;
        if (!_oper->argValue<double>("connectPointZ", connectionLocation[2])) return;
        //获取拾取类型
        bool isSeaModel = false;
        int seaType = 0, nodeID = -1;
        if (!this->getPickDiscDataType(pickObjID, isSeaModel, seaType)) return;
        if (!isSeaModel)
            if (!_oper->argValue<int>("NodeID", nodeID)) return;
        //创建新的数据
        if (isSeaModel && seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity) return;
        Acoustics::FITKLoadExcitationForceMoments* forceMoment = new Acoustics::FITKLoadExcitationForceMoments;
        _obj = forceMoment;
        forceMoment->setFENodeID(nodeID);
        if (!_obj) return;
        //设置连接器对象
        _obj->setConnectionObjID(pickObjID);
        _obj->setConnectionLocation(connectionLocation);
    }

    void GUILoadPointForceDialog::getDataFormWidget()
    {
        if (!_obj) return;
        bool isEnabledState = _ui->checkBox_EnabledState->checkState() == Qt::CheckState::Checked;
        _obj->setEnabledState(isEnabledState);
        //获取类型
        bool isSeaModel = false;
        int seaType = 0;
        if (!this->getPickDiscDataType(_obj->getConnectionObjID(), isSeaModel, seaType)) return;
        //设置数据
        if (isSeaModel)
            this->getSEALoadPointForceDataFormWidget();
        else
            this->getFELoadPointForceDataFormWidget();
    }

    void GUILoadPointForceDialog::getSEALoadPointForceDataFormWidget()
    {
        Acoustics::FITKLoadExcitationForceMoments* forceMoment = dynamic_cast<Acoustics::FITKLoadExcitationForceMoments*>(_obj);
        if (!forceMoment) return;
        //获取界面数据
        double fOriVec[3]{ 0,0,0 }, mOriVec[3]{ 0,0,0 }, connAxes[3]{ 0,0,0 };
        int forceSpectraID = _ui->comboBox_ForceSpectra->currentData().toInt();
        int momentSpectraID = _ui->comboBox_MomentSpectra->currentData().toInt();
        bool indirectRadiation = _ui->checkBox_IndirectRadiation->checkState() == Qt::CheckState::Checked;
        fOriVec[0] = _ui->lineEdit_F_Ori_X->text().toDouble();
        fOriVec[1] = _ui->lineEdit_F_Ori_Y->text().toDouble();
        fOriVec[2] = _ui->lineEdit_F_Ori_Z->text().toDouble();
        mOriVec[0] = _ui->lineEdit_M_Ori_X->text().toDouble();
        mOriVec[1] = _ui->lineEdit_M_Ori_Y->text().toDouble();
        mOriVec[2] = _ui->lineEdit_M_Ori_Z->text().toDouble();
        connAxes[0] = _ui->lineEdit_dx->text().toDouble();
        connAxes[1] = _ui->lineEdit_dy->text().toDouble();
        connAxes[2] = _ui->lineEdit_dz->text().toDouble();
        //设置数据
        forceMoment->setForceSpectraID(forceSpectraID);
        forceMoment->setMomentSpectraID(momentSpectraID);
        forceMoment->setForceOrientationVec(fOriVec);
        forceMoment->setMomentOrientationVec(mOriVec);
        forceMoment->setOffsetValue(connAxes);
        forceMoment->setIndirectRadiation(indirectRadiation);
    }

    void GUILoadPointForceDialog::getFELoadPointForceDataFormWidget()
    {
        Acoustics::FITKLoadExcitationForceMoments* forceMoment = dynamic_cast<Acoustics::FITKLoadExcitationForceMoments*>(_obj);
        if (!forceMoment) return;
        //获取界面数据
        double fOriVec[3]{ 0,0,0 }, mOriVec[3]{ 0,0,0 };
        int forceSpectraID = _ui->comboBox_ForceSpectra->currentData().toInt();
        int momentSpectraID = _ui->comboBox_MomentSpectra->currentData().toInt();
        fOriVec[0] = _ui->lineEdit_LxForce->text().toDouble();
        fOriVec[1] = _ui->lineEdit_LyForce->text().toDouble();
        fOriVec[2] = _ui->lineEdit_LzForce->text().toDouble();
        mOriVec[0] = _ui->lineEdit_LxMoment->text().toDouble();
        mOriVec[1] = _ui->lineEdit_LyMoment->text().toDouble();
        mOriVec[2] = _ui->lineEdit_LzMoment->text().toDouble();
        //设置数据
        forceMoment->setForceSpectraID(forceSpectraID);
        forceMoment->setMomentSpectraID(momentSpectraID);
        forceMoment->setForceOrientationVec(fOriVec);
        forceMoment->setMomentOrientationVec(mOriVec);
    }

    QString GUILoadPointForceDialog::getNameFormWidget()
    {
        return _ui->lineEdit_Name->text();
    }
}
